package org.tokaf.normalizer;

import java.util.HashMap;
import org.tokaf.TopKElement;

/**
 * <p>HashMapNormalizer contains HashMap of possible values represented by
 * TopKElements. Given the name of object, it finds it in the hashmap and
 * returns object's rating.</p> <p>Copyright (c) 2006</p>
 * @author Alan Eckhardt
 * @version 1.0
 */
public class HashMapNormalizer implements Normalizer {
	HashMap hm;

	String userName;

	boolean desc;

	public HashMapNormalizer(HashMap hm, boolean desc) {
		this.hm = hm;
		this.desc = desc;
	}

	public HashMapNormalizer(HashMap hm) {
		this.hm = hm;
		this.desc = true;
	}

	public HashMapNormalizer(HashMap hm, String userName) {
		this.hm = hm;
		this.desc = true;
		this.userName = userName;
	}

	public double Normalize(TopKElement te, int index) {
		if (te == null)
			return 0;
		return Normalize(te.getRatingObject(index));
	}

	public double Normalize(Object o) {
		if (o == null)
			return 0;
		Object oo = hm.get(o.toString());
		double res = -1;
		if (oo instanceof TopKElement) {
			res = ((TopKElement) oo).rating;
		} else if (oo instanceof Double) {
			res = ((Double) oo).doubleValue();
		}
		if (res == -1)
			return 0;
		if (desc)
			return res;
		return 1 - res;

	}

	public int compare(Object o1, Object o2) {
		double a = Normalize(o1);
		double b = Normalize(o2);
		if (a < b)
			return 1;
		if (a > b)
			return -1;
		return 0;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

}
